/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.fragment;

import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.view.View;

import com.bankscene.screenfit.activity.MainActivity;
import com.bankscene.screenfit.adapters.MenusListViewAdapter;
import com.bankscene.screenfit.model.Menus;
import com.bankscene.screenfit.util.Stack;
import com.bankscene.screenfit.events.EventBusEvents;
import com.bankscene.screenfit.vus.MenusListViewItemVu;
import com.bankscene.screenfit.vus.MenusListViewVu;

import org.greenrobot.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 子页面菜单导航条Presenter（针对pad使用）
 * Created by jiiiiiin on 6/14/16.
 */
public class MenusListViewFragment extends BasePresenterFragment<MenusListViewVu> {

    private Logger log = LoggerFactory.getLogger(MenusListViewFragment.class);
    private Menus chooseMenu;
    private Stack<View> chooseMenuConvertViews;

    @Override
    protected void init() {
        super.init();
        chooseMenuConvertViews = new Stack<>();
    }

    @Override
    protected void onBindVu() {
        vu.setListAdapter(MenusListViewAdapter.newInstance(bus));
        Bundle paramsBundle = getArguments();
        chooseMenu = (Menus) paramsBundle.get(MainActivity.MENU_ITEM);
    }

    @Override
    protected Class<MenusListViewVu> getVuClass() {
        return MenusListViewVu.class;
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe
    public void onBasePresenterAdapterGetView(EventBusEvents.OnBasePresenterAdapterGetViewEvent event) {
        if (chooseMenu.equals(Menus.getByPosition(event.position))) {
            // 只关注第一次的选中
            vu.updateItemStyleOnItemSelected(event.convertView);
            chooseMenuConvertViews.push(event.convertView);
        }
    }

    @Subscribe
    public void onItemClick(EventBusEvents.OnItemClickEvent event) {
        int position = event.position;
        Menus menus = Menus.getByPosition(position);
        if (!menus.equals(chooseMenu)) {
            vu.clearOldItemStyleOnItemSelected(chooseMenuConvertViews.peek());//先清除上一个选择的item样式
            chooseMenuConvertViews.push(vu.getSelectedView());
            vu.updateItemStyleOnItemSelected(chooseMenuConvertViews.peek());//恢复上一个选择的item样式

        }
    }

    @Override
    public boolean onBackPressed() {
        log.debug("f handleBackPressed {}", chooseMenuConvertViews.size());
        vu.clearOldItemStyleOnItemSelected(chooseMenuConvertViews.pop());//先清除上一个选择的item样式
        if (!chooseMenuConvertViews.empty()) {
            View convertView = chooseMenuConvertViews.peek();
            vu.updateItemStyleOnItemSelected(convertView);//恢复上一个选择的item样式
            MenusListViewItemVu menusListViewItemVu = (MenusListViewItemVu) convertView.getTag();
            getActivity().setTitle(menusListViewItemVu.getTitleView().getText());//更新标题
        }
        return super.onBackPressed();
    }

    @CheckResult
    public Stack<View> getChooseMenuConvertViews() {
        return chooseMenuConvertViews;
    }
}
